package com.uy.cuantoconocesdeuruguay.controllers;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.datatypes.FacebookDeviceUser;
import com.uy.cuantoconocesdeuruguay.datatypes.UserEnvelope;
import com.uy.cuantoconocesdeuruguay.exceptions.ConnectivityException;
import com.uy.cuantoconocesdeuruguay.server.HttpsExecuter;
import com.uy.cuantoconocesdeuruguay.server.ServerInfo;
import com.uy.cuantoconocesdeuruguay.utiles.NetworkUtils;

public class UsersController {
	private static final String SERVER_PREFIX = ServerInfo.SERVER_SECURE_HOST;
	private static final Gson gson = new Gson(); 

	private static final String DEVICE_ID_ = "deviceid=";
	private static final String OP_SET_FB_ID_ = "&operation=setFacebookId&facebookid=";
	private static final String OP_GET_USER_DATA_ = "&operation=getUserData&currentIP=";
	private static final String OP_GET_FRIENDS_SCORE_ = "&operation=getFriendsScores&listOfFriendsIds=";
	private static final String OP_GET_FRIEND_SCORE_ = "&operation=getFriendScore&friendId=";
	
	public static void facebookLogin(String facebookid) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = DEVICE_ID_ + deviceID + OP_SET_FB_ID_ + facebookid;
		HttpsExecuter.execute(completeURL, query, true);
	}
	public static UserEnvelope getUserData() throws ConnectivityException {
		String deviceID = MainActivity.userData.getDeviceID();		
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String ipv4 = NetworkUtils.getIPAddress();
		String query = DEVICE_ID_ + deviceID + OP_GET_USER_DATA_ + ipv4;
		String response = HttpsExecuter.execute(completeURL, query, true);
        try{    
        	UserEnvelope ret = gson.fromJson(response, UserEnvelope.class);
	        return ret;  
        }catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
        	e.printStackTrace(); 
        	return null;
        }
	}
	public static List<FacebookDeviceUser> getFriendsScores(String listOfFriendsIds) throws ConnectivityException  {
		String deviceID = MainActivity.userData.getDeviceID();		
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = DEVICE_ID_ + deviceID + OP_GET_FRIENDS_SCORE_ + listOfFriendsIds;
		String response = HttpsExecuter.execute(completeURL, query, true);
        try{    
        	Type t = new TypeToken<List<FacebookDeviceUser>>(){}.getType();
            List<FacebookDeviceUser> ret = gson.fromJson(response, t);
	        return ret; 
        }catch (Exception e){
			GoogleAnalyticsTracker.trackException(MainActivity.instance, e);
        	e.printStackTrace(); 
        	return null;
        }
	}
	public static FacebookDeviceUser getFriendScore(String friendId) throws ConnectivityException {
		String deviceID = MainActivity.userData.getDeviceID();		
		String completeURL = SERVER_PREFIX+ServerInfo.USERS_PATH;
		String query = DEVICE_ID_ + deviceID + OP_GET_FRIEND_SCORE_ + friendId;
		String response = HttpsExecuter.execute(completeURL, query, true);
		FacebookDeviceUser facebookDeviceUser = gson.fromJson(response, FacebookDeviceUser.class);
		return facebookDeviceUser;
	}
}
